layui.use(['form', 'table', 'layarea', 'tableSelect'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layarea = layui.layarea;

      let from_client=$.getQueryVariable('from_client')
      let audit_status=$.getQueryVariable('audit_status')


    /*城市级联*/
    layarea.render({
        elem: '#area-picker',
        change: function (res) {
            //选择结果
            submitInfo();
        }
    });
    /*列表*/
    table.render({
        elem: '#currentTableId',
        url: '/admin/Supplier/index?action=ajax&audit_status='+audit_status+'&from_client='+from_client,
        method: 'post',
        parseData: function (res) { //res 即为原始返回的数据
            $('#countNum').html(res.count);
        },
        // 名称 分类 联系人 联系方式 应收应付 备注 状态
        toolbar: '#toolbarDemo',
        defaultToolbar: [ 'exports'],
        cols: [[
            {field: 'supplier_name', title: '名称' },
            {field: 'relation_name', title: '联系人'},
            {field: 'relation_phone', title: '联系方式',  },
            {field: 'pay_price', title: '应收应付', templet:'#price',sort:true},
            {field: 'desc', title: '备注'},
            {title: '操作', toolbar: '#currentTableBar'}
        ]],
        limits: [10, 15, 20, 25, 30, 50, 100],
        limit: 10,
        page: true,
        skin: 'line,row',

    });
    //编辑和删除
    table.on('tool(currentTableFilter)', function (obj) {
        var data = obj.data;
        if (obj.event === 'edit') {
            var index = layer.open({
                title: '供应商详情',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '/admin/Supplier/saveinfo?id=' + data.id,
                end: function () {
                    /*删除session无用图片*/
                    $.ajax({
                        url: '/admin/Supplier/clearSession',
                        type: 'post',
                        dataType: 'json',
                    });
                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        }

        if (obj.event === 'delete') {

            layer.confirm('真的删除行么', function (index) {


                $.ajax({
                    url: '/admin/supplier/DelInfo',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        id: obj.data.id,
                    },
                    success: function(data) {

                        if (data.status) {
                            layer.msg('删除成功!', {icon: 1, time: 1000});
                            $(obj)[0].tr.remove()

                        }else{
                            layer.msg(data.msg, {
                                icon: 2,
                                time: 3000
                            });
                        }
                    },
                    error: function(re) {
                        console.log(re)
                        layer.open({
                            title: '删除失败',
                            content: '<span style="color:red">'+re.responseJSON.errorCode+':</span>'+re.responseJSON.msg
                        })
                    }
                });


                // $(obj)[0].tr.remove()
            });
        }
    });
    //添加
    table.on('toolbar(currentTableFilter)', function (obj) {

        if (obj.event === 'add') {  // 监听添加操作
            let index = layer.open({
                title: '添加',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '/admin/supplier/Saveinfo'
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        }
    });

    // 监听搜索操作 24754283
    // form.on('submit(data-search-btn)', function (data) {
    //     submitInfo();
    // });
    // 监听搜索操作
    form.on('submit(data-search-btn)', function (data) {
        //执行搜索重载
        table.reload('currentTableId', {
            page: {
                curr: 1
            }
            , where: {
                param: data.field
            }
        }, 'data');

        return false;
    });
    form.on('select(selectInfo)', function (data) {
        $('#formbutton').click();
    });
    // form.on('select(selectInfo)', function (data) {
    //     submitInfo();
    // });




    function submitInfo() {
        var param = {};
        var t = $('#formInfo [name]');
        console.log(param)
        $.each(t, function () {
            param[this.name] = this.value;
        });
        //var result = JSON.stringify(d);

        //执行搜索重载
        table.reload('currentTableId', {
            page: {
                curr: 1
            }
            , where: {
                //searchParams: result
                param
            }
        }, 'data');
        return false;
    };
});
